package com.iap.client.mediator
{
	import com.hillelcoren.components.CloseableTitleWindow;
	import com.iap.app.AppGlobal;
	import com.iap.client.event.FillerEvent;
	import com.iap.client.view.favorite.FavoriteEditComboWindow;
	import com.iap.client.view.favorite.FavoriteEditWindow;
	import com.iap.common.event.FavoriteEvent;
	import com.iap.common.event.InstanceFavoriteEvent;
	import com.iap.common.event.SampleEvent;
	import com.iap.common.event.SampleFillerEvent;
	import com.iap.common.mediator.BaseMediator;
	import com.iap.common.model.Favorite;
	import com.iap.common.model.Filler;
	import com.iap.common.model.InstanceFavorite;
	import com.iap.common.model.Sample;
	import com.iap.common.signal.FavoriteRequestSignal;
	import com.iap.common.signal.FillerRequestSignal;
	import com.iap.common.signal.InstanceFavoriteRequestSignal;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import flashx.textLayout.events.ModelChange;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	public class FavoriteEditWindowMediator extends BaseMediator
	{
		[Inject]
		public var view:FavoriteEditWindow;
		[Inject]
		public var fillerRequest:FillerRequestSignal;
		[Inject]
		public var instanceFavoriteRequest:InstanceFavoriteRequestSignal;
		
		private var comboWindow:FavoriteEditComboWindow;
		
		private var model:Sample;

		override public function onRegister():void
		{
			addContextListener(FillerEvent.RESPONSE, fillerResponseHandler);
			addContextListener(FavoriteEvent.RESPONSE, favoriteResponseHandler);
			addContextListener(InstanceFavoriteEvent.RESPONSE, instanceFavoriteResponseHandler);
			view.layerDisplay.addEventListener(SampleFillerEvent.CHANGE, fillerChangeHandler);
			view.saveButton.addEventListener(MouseEvent.CLICK, saveHandler);
			view.combineButton.addEventListener(MouseEvent.CLICK, combineHanlder);
			dispatchRequest();
		}
		
		private function dispatchRequest():void
		{
			AppGlobal.startWait();
			fillerRequest.dispatch(new Filler(), FillerRequestSignal.GET);
		}

		private function fillerResponseHandler(evt:FillerEvent):void
		{
			AppGlobal.endWait();
			var fillers:Array = evt.response.result as Array;
			var solidColors:Array = fillers.filter(filterFillerBySolid);
			var textures:Array = fillers.filter(filterFillerByTexture);
			view.fillerSolidDisplay.dataProvider = new ArrayCollection(solidColors);
			view.fillerTextureDisplay.dataProvider = new ArrayCollection(textures);
		}

		private function filterFillerBySolid(element:*, index:int, arr:Array):Boolean
		{
			return element.type == 0;
		}

		private function filterFillerByTexture(element:*, index:int, arr:Array):Boolean
		{
			return element.type == 1;
		}
		
		private function fillerChangeHandler(evt:SampleFillerEvent):void
		{
			view.display.setSampleFiller(evt.model);
			view.setSaveEnabled();
		}
		
		private function saveHandler(evt:Event):void
		{
			view.saveButton.enabled = false;
			saveFavorite();
		}
		
		private function saveFavorite():void
		{
			var sample:Sample = view.model.favorite.sample;
			sample.name = sample.name == null ? "" : sample.name;
			sample.sampleFillers = view.display.sampleFillers;

			var favorite:Favorite = view.model.favorite;
			favorite.title = view.titleInput.text;
			favorite.sample = sample;
			
			var instanceFavorite:InstanceFavorite = view.model;
			instanceFavorite.favorite = favorite;

			instanceFavoriteRequest.dispatch(instanceFavorite, InstanceFavoriteRequestSignal.SAVE);
		}
		
		private function favoriteResponseHandler(evt:FavoriteEvent):void
		{
			if(evt.response.task == FavoriteRequestSignal.SAVE)
			{
				view.dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
			}
		}

		private function instanceFavoriteResponseHandler(evt:InstanceFavoriteEvent):void
		{
			if(evt.response.task == InstanceFavoriteRequestSignal.SAVE)
			{
				view.dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
			}
		}
		
		private function combineHanlder(evt:Event):void
		{
			var favoriteModel:Favorite = view.model.favorite;
			
			var sample:Sample = new Sample();
			sample.vectorModel = favoriteModel.sample.vectorModel;
			var favorite:Favorite = new Favorite();
			favorite.sample = sample;
			favorite.title = favoriteModel.title;

			comboWindow = new FavoriteEditComboWindow();
			comboWindow.model = favorite;
			PopUpManager.addPopUp(comboWindow, view.root, true);
			PopUpManager.centerPopUp(comboWindow);
			comboWindow.addEventListener(CloseEvent.CLOSE, comboCloseHandler);
			view.root.addEventListener(Event.RESIZE, resizeHandler);
		}

		private function resizeHandler(evt:Event):void
		{
			PopUpManager.centerPopUp(comboWindow);
		}
		
		private function comboCloseHandler(evt:Event):void
		{
			view.root.removeEventListener(Event.RESIZE, resizeHandler);
			comboWindow.removeEventListener(CloseEvent.CLOSE, comboCloseHandler);
		}
	}
}